<?php

namespace app\common\model;

use think\Model;
use think\Db;

class LcUser extends Model {

    public function insertUser($data) {
        //这没有加锁，可能会出错invite_code重复的错
        if(!isset($data['invite_code'])){
            $max_id = $this->max('id');
            $uid = (int)$max_id + 1;
            $data['invite_code'] = createCode($uid);
        }
        $pid = isset($data['pid'])?$data['pid']:1;
        if($data['username']=='root'){
            $pid = 0;
        }
        $result = Db::query('call pro_insert_user(:pid,:username,:phone,:password,:auth_phone,:ip,:time_zone,:logintime,:act_time,:time,:invite_code)', [
            'pid' => $pid,
            'username' => $data['username'],
            'phone' => isset($data['phone'])?$data['phone']:null,
            'password' => isset($data['password'])?$data['password']:'',
            'auth_phone' => isset($data['auth_phone'])?$data['auth_phone']:0,
            'ip' => isset($data['ip'])?$data['ip']:'',
            'time_zone' => isset($data['time_zone'])?$data['time_zone']:'',
            'logintime' => isset($data['logintime'])?$data['logintime']:'',
            'act_time' => isset($data['act_time'])?$data['act_time']:'',
            'time' => isset($data['time'])?$data['time']:'',
            'invite_code'=>$data['invite_code'],
        ]);
        if(!empty($result)){
            return $result[0][0]['user_id'];
        }
        return 0;
    }

    public function deleteUser($id) {
        $result = Db::execute('call pro_delete_user(:id)', [
            'id' => $id,
        ]);
        return $result;
    }
}